#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    typedef struct ListNode ListNode, * pListNode;
    pListNode fast = head;
    pListNode slow = head;
    pListNode free = (pListNode)malloc(sizeof(ListNode));
    pListNode prev = free;
    prev->next = head;
    while (n--)
    {
        fast = fast->next;
    }
    while (fast)
    {
        fast = fast->next;
        slow = slow->next;
        prev = prev->next;
    }
    //free(free);
    if (slow == head)
    {
        if (slow->next == NULL)
        {
            //free(slow);
            return NULL;
        }
        else
        {
            slow = slow->next;
            //free(head);
            return slow;
        }
    }
    else
    {
        prev->next = slow->next;
        //free(slow);
        return head;
    }
}